//
//  UIView+WSQBadge.m
//  Mermaid Trip customer
//
//  Created by 翁胜琼 on 2019/2/16.
//  Copyright © 2019 宁波神凤海洋世界有限公司. All rights reserved.
//  

#import "UIView+WSQBadge.h"
#import <objc/runtime.h>

@implementation UIView (WSQBadge)


- (void)showBadgeWithTopPadding:(CGFloat)topPadding
                   rightPadding:(CGFloat)rightPadding
{
    if (self.sq_badgeLabel == nil) {

        self.sq_badgeLabel = [[UILabel alloc] init];
        self.sq_badgeLabel.textAlignment = NSTextAlignmentCenter;
        
        [self setupLabelAppearanceWithBadgeValue:self.sq_badgeValue
                                        fontName:self.sq_badgeFontname
                                        fontsize:self.sq_badgeFontSize
                                       fontColor:self.sq_badgeFontColor
                                     borderWidth:self.sq_badgeBorderWidth
                                     borderColor:self.sq_badgeBorderColor
                                 backgroundColor:self.sq_badgeBackgroundColor
                                      topPadding:topPadding
                                    rightPadding:rightPadding];
        
        [self addSubview:self.sq_badgeLabel];
        [self bringSubviewToFront:self.sq_badgeLabel];
    }
}

- (void)hidenBadge
{
    //从父视图上面移除
    [self.sq_badgeLabel removeFromSuperview];
}

// 指定更新方法
- (void)setupLabelAppearanceWithBadgeValue:(NSString *)badgeValue
                                  fontName:(NSString *)fontName
                                  fontsize:(CGFloat)fontSize
                                 fontColor:(UIColor *)fontColor
                               borderWidth:(CGFloat)borderWidth
                               borderColor:(UIColor *)borderColor
                           backgroundColor:(UIColor *)backgroundColor
                                topPadding:(CGFloat)topPadding
                              rightPadding:(CGFloat)rightPadding
{
    UIFont *font = [UIFont fontWithName:fontName size:fontSize];
    
    self.sq_topPadding = topPadding;
    self.sq_rightPadding = rightPadding;
    
    self.sq_badgeLabel.text = badgeValue;
    self.sq_badgeLabel.font = font;
    self.sq_badgeLabel.textColor = fontColor;
    self.sq_badgeLabel.layer.borderWidth = borderWidth;
    self.sq_badgeLabel.layer.borderColor = borderColor.CGColor;
    self.sq_badgeLabel.backgroundColor = backgroundColor;
    self.sq_badgeLabel.layer.masksToBounds = borderWidth > 0;
    
    [self.sq_badgeLabel sizeToFit];
    
    CGFloat labelWidth = self.sq_badgeLabel.bounds.size.width;
    CGFloat labelHeight = self.sq_badgeLabel.bounds.size.height;
    
    CGRect realFrame = CGRectMake(CGRectGetWidth(self.frame) - rightPadding - labelWidth +4, topPadding, labelWidth+4, labelHeight);
    self.sq_badgeLabel.frame = realFrame;
    
    if (labelWidth < labelHeight)
    {
        self.sq_badgeLabel.frame = CGRectMake(CGRectGetWidth(self.frame) - rightPadding - labelHeight, topPadding, labelHeight, labelHeight);
    }
    
    CGFloat cornerRadius = labelHeight / 2;
    self.sq_badgeLabel.layer.cornerRadius = cornerRadius;
}

- (void)setupLabelAppearanceWithBadgeValue:(NSString *)badgeValue
                                  fontName:(NSString *)fontName
                                  fontsize:(CGFloat)fontSize
                                 fontColor:(UIColor *)fontColor
                               borderWidth:(CGFloat)borderWidth
                               borderColor:(UIColor *)borderColor
                           backgroundColor:(UIColor *)backgroundColor
{
    [self setupLabelAppearanceWithBadgeValue:badgeValue
                                    fontName:fontName
                                    fontsize:fontSize
                                   fontColor:fontColor
                                 borderWidth:borderWidth
                                 borderColor:borderColor
                             backgroundColor:backgroundColor
                                  topPadding:self.sq_topPadding
                                rightPadding:self.sq_rightPadding];
}

// 更新label样式
- (void)updateLabelWithBadgeValue:(NSString *)badgeValue
                         FontName:(NSString *)fontName
                         fontSize:(CGFloat)fontSize

{
    [self setupLabelAppearanceWithBadgeValue:badgeValue
                                    fontName:fontName
                                    fontsize:fontSize
                                   fontColor:self.sq_badgeFontColor
                                 borderWidth:self.sq_badgeBorderWidth
                                 borderColor:self.sq_badgeBorderColor
                             backgroundColor:self.sq_badgeBackgroundColor];
}

#pragma mark - GetterAndSetter

- (UILabel *)sq_badgeLabel
{
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setSq_badgeLabel:(UILabel *)badgeLabel
{
    objc_setAssociatedObject(self, @selector(sq_badgeLabel), badgeLabel, OBJC_ASSOCIATION_RETAIN);
}

- (NSString *)sq_badgeFontname
{
    NSString *fontName = objc_getAssociatedObject(self,_cmd);
    fontName = fontName.length ==0? @"PingFangSC-Light" : fontName;
    
    return fontName;
}

- (void)setSq_badgeFontname:(NSString *)sq_badgeFontname
{
    [self updateLabelWithBadgeValue:self.sq_badgeValue FontName:sq_badgeFontname fontSize:self.sq_badgeFontSize];
    objc_setAssociatedObject(self, @selector(sq_badgeFontname), sq_badgeFontname, OBJC_ASSOCIATION_COPY);
}

- (CGFloat)sq_badgeFontSize
{
    CGFloat fontSize = [objc_getAssociatedObject(self,_cmd) floatValue];
    fontSize = fontSize == 0? 9:fontSize;
    
    return fontSize;
}

- (void)setSq_badgeFontSize:(CGFloat)sq_badgeFontSize
{
    [self updateLabelWithBadgeValue:self.sq_badgeValue FontName:self.sq_badgeFontname fontSize:sq_badgeFontSize];
    objc_setAssociatedObject(self, @selector(sq_badgeFontSize), @(sq_badgeFontSize), OBJC_ASSOCIATION_ASSIGN);
}

- (NSString *)sq_badgeValue
{
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setSq_badgeValue:(NSString *)sq_badgeValue
{
    [self updateLabelWithBadgeValue:sq_badgeValue FontName:self.sq_badgeFontname fontSize:self.sq_badgeFontSize];
    objc_setAssociatedObject(self, @selector(sq_badgeValue), sq_badgeValue, OBJC_ASSOCIATION_COPY);
}

- (UIColor *)sq_badgeFontColor
{
    UIColor *fontColor = objc_getAssociatedObject(self, _cmd);
    fontColor = fontColor == nil? [UIColor whiteColor]:fontColor;
    return fontColor;
}

- (void)setSq_badgeFontColor:(UIColor *)sq_badgeFontColor
{
    self.sq_badgeLabel.textColor = sq_badgeFontColor;
    objc_setAssociatedObject(self, @selector(sq_badgeFontColor), sq_badgeFontColor, OBJC_ASSOCIATION_RETAIN);
}

- (CGFloat)sq_badgeBorderWidth
{
    return [objc_getAssociatedObject(self, _cmd) floatValue];
}

- (void)setSq_badgeBorderWidth:(CGFloat)sq_badgeBorderWidth
{
    self.sq_badgeLabel.layer.borderWidth = sq_badgeBorderWidth;
    self.sq_badgeLabel.layer.masksToBounds = sq_badgeBorderWidth > 0;
    
    objc_setAssociatedObject(self, @selector(sq_badgeBorderWidth), @(sq_badgeBorderWidth), OBJC_ASSOCIATION_ASSIGN);
}

- (UIColor *)sq_badgeBorderColor
{
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setSq_badgeBorderColor:(UIColor *)sq_badgeBorderColor
{
    self.sq_badgeLabel.layer.borderColor = sq_badgeBorderColor.CGColor;
    objc_setAssociatedObject(self, @selector(sq_badgeBorderColor), sq_badgeBorderColor, OBJC_ASSOCIATION_RETAIN);
}

- (UIColor *)sq_badgeBackgroundColor
{
    UIColor *backgroundColor = objc_getAssociatedObject(self, _cmd);
    backgroundColor = backgroundColor ==nil? [UIColor redColor] : backgroundColor;
    return backgroundColor;
}

- (void)setSq_badgeBackgroundColor:(UIColor *)sq_badgeBackgroundColor
{
    self.sq_badgeLabel.backgroundColor = sq_badgeBackgroundColor;
    objc_setAssociatedObject(self, @selector(sq_badgeBackgroundColor), sq_badgeBackgroundColor, OBJC_ASSOCIATION_RETAIN);
}

- (CGFloat)sq_topPadding
{
    CGFloat padding = [objc_getAssociatedObject(self, _cmd) floatValue];
    return padding;
}

- (void)setSq_topPadding:(CGFloat)toppadding
{
    objc_setAssociatedObject(self, @selector(sq_topPadding), @(toppadding), OBJC_ASSOCIATION_ASSIGN);
}

- (CGFloat)sq_rightPadding
{
    CGFloat padding = [objc_getAssociatedObject(self, _cmd) floatValue];
    return padding;
}

- (void)setSq_rightPadding:(CGFloat)rightPadding
{
    objc_setAssociatedObject(self, @selector(sq_rightPadding), @(rightPadding), OBJC_ASSOCIATION_ASSIGN);
}

@end

